package com.qf.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.qf.dto.ElefeeDto;
import com.qf.entity.Elefee;
import com.qf.entity.Waterfee;
import com.qf.result.ResponseResult;
import com.qf.service.ElefeeService;
import com.qf.utils.BeanCopyUtils;
import com.qf.utils.MailUtil;
import com.qf.utils.PayUtil;
import com.qf.utils.Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.GenericServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/elefee")
@Slf4j
public class ElefeeController {

    @Autowired
    private ElefeeService elefeeService;

    @GetMapping("/test")
    public ResponseResult test(){
        return elefeeService.test();
    }

    @Autowired
    PayUtil payUtil;

    @Autowired
    MailUtil mailUtil;

    @PostMapping("/pay")
    public ResponseResult pay(@RequestBody Elefee elefee, HttpServletRequest request, HttpServletResponse response){
//        Double waterFee = waterfee.getWaterFee();
        Double eleFee = elefee.getEleFee();
        try {
            String alipay = payUtil.alipay(request, response, eleFee ,elefee.getId());

            return ResponseResult.okResult(alipay);
        }catch (Exception e){
            return ResponseResult.errorResult(201,"支付失败");
        }
//        return ResultBean.FAIL();
    }

//    @PostMapping("/notify")
//    public void notifyUrl(HttpServletRequest request) throws AlipayApiException {
//
//        //先验签,
//        Map<String, String> paramsMap = Utils.convertRequestParamsToMap(request); //将异步通知中收到的所有参数都存放到 map 中
//
//        //
//        log.info(paramsMap+"====================");
//
//        boolean  signVerified = AlipaySignature.rsaCheckV1(paramsMap, payUtil.getAPP_PRIVATE_KEY(), "utf-8", "RSA2");  //调用SDK验证签名
//        //验签成功 再根据支付修改订单状态
//        if (signVerified){
//            // TODO 验签成功后，按照支付结果异步通知中的描述，对支付结果中的业务内容进行二次校验，校验成功后在response中返回success并继续商家自身业务处理，校验失败返回failure
//            //购买成功,修改数据库
////            UpdateWrapper<User> wrapper = new UpdateWrapper<>();
////            wrapper.set("account",)
//
//
//        } else {
//            // TODO 验签失败则记录异常日志，并在response中返回failure.
//        }
//    }


    @GetMapping("/findAll")
    public ResponseResult findAll(String ownerName){
        return elefeeService.findAll(ownerName);
    }

    @DeleteMapping("/delete/{id}")
    public ResponseResult delete(@PathVariable Integer id){
        return elefeeService.delete(id);
    }

    @PostMapping("/add")
    public ResponseResult add(@RequestBody @Valid ElefeeDto elefee){
        //Date date = new Date();
       // SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        System.out.println(dateFormat.format(date));

        elefee.setCreateTime(new Date());
        elefee.setFeeStatus("0");
        return elefeeService.add(BeanCopyUtils. copyBean(elefee,Elefee.class));
    }

    @GetMapping("/findAllHouse")
    public ResponseResult findAllHouse(){
        return elefeeService.findAllHouse();
    }

    @GetMapping("/findOwnerByHouseId")
    public ResponseResult findOwnerByHouseId(String house){
        return elefeeService.findOwnerByHouseId(house);
    }
}
